package top.yokey.android.base;

import android.app.Application;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;

import androidx.core.content.ContextCompat;

import java.util.HashMap;

import top.yokey.android.R;
import top.yokey.android.http.HttpHelp;
import top.yokey.android.http.MemberData;
import top.yokey.android.view.PullRefreshViewHelper;

//程序运行的时候，一定会走到这里，而且是第一个类走这
public class App extends Application {

    private static App instance = null;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private MemberData memberData = null;

    public static App get() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //第三方框架，自定义的框架初始化一般放在 Applicaiton 类下的 oncreate 方法
        //初始化Share轻量级存储
        sharedPreferences = getSharedPreferences("android", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        //初始化网络请求框架
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("model", Build.MODEL);
        hashMap.put("token", sharedPreferences.getString("token", ""));
        HttpHelp.get().init(this, "http://192.168.8.61/api/", hashMap);
        //下拉刷新
        PullRefreshViewHelper.get().init(16);//设置文本的大小
        //设置下拉刷新的颜色
        PullRefreshViewHelper.get().setColors(ContextCompat.getColor(get(),R.color.primary));
        //设置加载中的风格样式
        PullRefreshViewHelper.get().setLoadStyle(PullRefreshViewHelper.LOAD_STYLE_PULSE);
        PullRefreshViewHelper.get().setLoadColor(ContextCompat.getColor(get(),R.color.primary));
        PullRefreshViewHelper.get().setLoadText("加载中~");
        //列表为空时显示的图标及文本
        PullRefreshViewHelper.get().setEmptyImage(R.mipmap.ic_pull_refresh_empty);
        PullRefreshViewHelper.get().setEmptyColor(Color.GRAY);
        PullRefreshViewHelper.get().setEmptyText("暂无内容，请稍后再试");
        //列表加载错误时显示的图标及文本
        PullRefreshViewHelper.get().setErrorImage(R.mipmap.ic_pull_refresh_error);
        PullRefreshViewHelper.get().setErrorColor(Color.GRAY);
        PullRefreshViewHelper.get().setErrorText("加载失败，请稍后再试~");

    }

    //alt + insert 选择 getter and setter
    public SharedPreferences getSharedPreferences() {
        return sharedPreferences;
    }

    public void setSharedPreferences(SharedPreferences sharedPreferences) {
        this.sharedPreferences = sharedPreferences;
    }

    public SharedPreferences.Editor getEditor() {
        return editor;
    }

    public void setEditor(SharedPreferences.Editor editor) {
        this.editor = editor;
    }

    public MemberData getMemberData() {
        return memberData;
    }

    public void setMemberData(MemberData memberData) {
        this.memberData = memberData;
    }

}
